package com.qing.utils.lottery.ssq;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.qing.utils.lottery.ssq.bean.SsqResponse;
import com.qing.utils.lottery.ssq.bean.SsqResponseResult;
import lombok.Data;
import org.apache.logging.log4j.core.net.Priority;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SsqAnalyzer {


    @Data
    static class SsqInfo {
        private int[] reds = new int[6];

        private int blue;

        private String data;
        private String redData;
        private String blueData;

        private String date;

        private int total;


        public void parseRed(String redGroup) {
            String[] split = redGroup.split(",");
            for (int i = 0; i < split.length; i++) {
                reds[i] = Integer.parseInt(split[i].trim());
            }
        }


    }


    static class MultiValueHashMap<K, V> extends HashMap<K, List<V>> {

        public void addValue(K key, V value) {
            List<V> vs = this.computeIfAbsent(key, k -> new ArrayList<>());
            vs.add(value);
        }

    }

    private static String path = "D:\\Mine\\MySpaces\\Gitee\\common-utils\\lottery-utils\\src\\main\\resources\\ssqResponse\\ssqResponse_2023-08-19_18_09_10";

    public static void main(String[] args) {
        JSON json = JSONUtil.readJSON(new File(path), StandardCharsets.UTF_8);
        SsqResponse ssqResponse = json.toBean(SsqResponse.class);
        List<SsqResponseResult> result = ssqResponse.getResult();

        List<SsqInfo> infos = doAnalyze(result);
        analyzeDuplicate(infos);
    }

    private static List<SsqInfo> doAnalyze(List<SsqResponseResult> resultList) {

        int size = resultList.size();
        List<SsqInfo> infos = new ArrayList<>(size);
        System.out.println("红色球开奖记录: " + size);
        for (SsqResponseResult result : resultList) {
            String reds = result.getRed();
            String blue = result.getBlue();

            SsqInfo ssqInfo = new SsqInfo();
            ssqInfo.setBlue(Integer.parseInt(blue.trim()));
            ssqInfo.parseRed(reds);
            ssqInfo.setData(reds + "/" + blue);
            ssqInfo.setRedData(reds);
            ssqInfo.setBlueData(blue);
            ssqInfo.setDate(result.getDate());
            ssqInfo.setTotal(size);
            infos.add(ssqInfo);
        }


        return infos;
    }

    private static void analyzeDuplicate(List<SsqInfo> infos) {

        MultiValueHashMap<String, SsqInfo> mValues = new MultiValueHashMap<>();

        MultiValueHashMap<String, SsqInfo> mRedValues = new MultiValueHashMap<>();

        MultiValueHashMap<String, SsqInfo> mBlueValues = new MultiValueHashMap<>();


        for (SsqInfo info : infos) {
            mValues.addValue(info.getData(), info);
            mRedValues.addValue(info.getRedData(), info);
            mBlueValues.addValue(info.getBlueData(), info);

        }


        analyseDuplicate(mValues);

        analyseRedDuplicate(mRedValues);
        analyseBlueDuplicate(mBlueValues);
    }

    private static void analyseDuplicate(MultiValueHashMap<String, SsqInfo> mValues) {
        System.out.println("开始分析重复开奖号码");
        int dup = 0;
        Set<Map.Entry<String, List<SsqInfo>>> entries = mValues.entrySet();
        for (Map.Entry<String, List<SsqInfo>> entry : entries) {
            List<SsqInfo> value = entry.getValue();
            int size = value.size();
            if (size > 1) {
                dup++;
                System.out.print("重复号码: " + entry.getKey() + ", 重复次数: " + size + ", 重复概率: " + (((float)size*100)/value.get(0).total) + ", 日期: ");
                for (int i = 0; i < size; i++) {
                    SsqInfo ssqInfo = value.get(i);
                    System.out.print(i + ": " + ssqInfo.getDate() + ";   ");
                }
                System.out.println();
            }
        }
        System.out.println("有" + dup + "个重复开奖号码");
    }


    private static void analyseRedDuplicate(MultiValueHashMap<String, SsqInfo> mValues) {
        System.out.println("开始分析重复开奖红球号码");
        int dup = 0;

        Set<Map.Entry<String, List<SsqInfo>>> entries = mValues.entrySet();
        for (Map.Entry<String, List<SsqInfo>> entry : entries) {
            List<SsqInfo> value = entry.getValue();
            int size = value.size();
            if (size > 1) {
                dup++;
                System.out.print("重复号码: " + entry.getKey() + ", 重复次数: " + size + ", 重复概率: " + (((float)size*100)/value.get(0).total) + ", 日期: ");
                for (int i = 0; i < size; i++) {
                    SsqInfo ssqInfo = value.get(i);
                    System.out.print(i + ": " + ssqInfo.getDate() + ";   ");
                }
                System.out.println();
            }
        }
        System.out.println("有" + dup + "个重复开奖红球号码");
    }

    private static void analyseBlueDuplicate(MultiValueHashMap<String, SsqInfo> mValues) {
        System.out.println("开始分析重复开奖蓝球号码");
        int dup = 0;

        Set<Map.Entry<String, List<SsqInfo>>> entries = mValues.entrySet();
        for (Map.Entry<String, List<SsqInfo>> entry : entries) {
            List<SsqInfo> value = entry.getValue();
            int size = value.size();
            if (size > 1) {
                dup++;
                System.out.print("重复号码: " + entry.getKey() + ", 重复次数: " + size + ", 重复概率: " + (((float)size*100)/value.get(0).total) + ", 日期: ");
                for (int i = 0; i < size; i++) {
                    SsqInfo ssqInfo = value.get(i);
                    System.out.print(i + ": " + ssqInfo.getDate() + ";   ");
                }
                System.out.println();
            }
        }

        System.out.println("有" + dup + "个重复开奖蓝球号码");
    }

}
